using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AutoGear.BLL;
using ECommerce.Library.DataStructure;
using ECommerce.Library.Web;
using System.Collections.Generic;
using ECommerce.Library;
using System.IO;
using Ext.Net;

public partial class admin_customer : SiteAdminBasePage
{
    private bool _useSearch
    {
        get
        {
            if (Session["_useSearch"] == null)
                return false;
            else return Convert.ToBoolean(Session["_useSearch"]);
        }
        set
        {
            Session["_useSearch"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        uccResponseMessage.Visible = false;

        if (!X.IsAjaxRequest)
        {
            Session["_useSearch"] = null;
        }
    }

    protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
    {
        BindCustomers(String.IsNullOrEmpty(e.Sort) ? "UserName" : e.Sort + (e.Dir.ToString() == "Default" ? "" : " " + e.Dir), e.Start, e.Limit);
    }

    protected void imgSearchButton_Click(object sender, EventArgs e)
    {

    }

    private void BindCustomers(string sort, int start, int limit)
    {
        ReadWriteCustomCollection<CustomerUser> pagedCustomer = null;
        int totalCount = 0;
        start = start * limit;

        if (this._useSearch)
        {
            string _searchCustomerName = txtToolbarSearch.Text;
            pagedCustomer = CustomerUser.GetCustomerUser(_searchCustomerName, sort, start, limit, false);
            totalCount = CustomerUser.TotalCustomerCount(_searchCustomerName, false);
            Session["_useSearch"] = null;
        }
        else
        {
            pagedCustomer = CustomerUser.GetCustomerUser("",sort, start, limit,false);
            totalCount = CustomerUser.TotalCustomerCount("", false);
           // totalCount = pagedCustomer.Count;
        }

        Store1.DataSource = pagedCustomer;
        Store1.DataBind();

        (Store1.Proxy[0] as PageProxy).Total = totalCount;
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void UpdateCustomer(int id, string email)
    {
        CustomerUser customer = CustomerUser.GetCustomerUserByUserId(id);
        customer.Profile.EmailAddress = email;
        customer.Update();
        //Store1.DataBind();
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void DeleteCustomers(string ids)
    {
        ids = ids.TrimStart(' ');
        string[] idArray = ids.Split(' ');
        List<int> idList = new List<int>();
        foreach (string id in idArray) idList.Add(Convert.ToInt32(id));
        CustomerUser.DeleteCustomerUser(idList);
        Store1.DataBind();
    }

    [DirectMethod(Namespace = "Polana.WebService")]
    public void SearchCustomer()
    {
        _useSearch = true;
        Store1.DataBind();
    }
}
